﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Quan_Ly_Phong_Kham_co_len_em
{
    public partial class frmtiepdon : Form
    {
        public static string _mabn;
        public frmtiepdon()
        {
            InitializeComponent();
            DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
           
             
            TV_Tonghop.Themcot("MA_BN", "Mã Bệnh Nhân", dt_tiepdoan, DataGridViewAutoSizeColumnMode.Fill);
            TV_Tonghop.Themcot("HOTEN_BN", "Họ Tên Bệnh Nhân", dt_tiepdoan, DataGridViewAutoSizeColumnMode.Fill);
            TV_Tonghop.Themcot("Gioitinh", "Giới Tính ", dt_tiepdoan, DataGridViewAutoSizeColumnMode.Fill);
            TV_Tonghop.Themcot("NAMSINH_BN", "Năm Sinh ", dt_tiepdoan, DataGridViewAutoSizeColumnMode.Fill);
            TV_Tonghop.Themcot("BAO_HIEM", "Bảo Hiểm ", dt_tiepdoan, DataGridViewAutoSizeColumnMode.Fill);
            TV_Tonghop.Themcot("Ten", "Loại Bảo Hiểm ", dt_tiepdoan, DataGridViewAutoSizeColumnMode.Fill);
            TV_Tonghop.Themcot("DIACHI_BN", "Địa Chỉ ", dt_tiepdoan, DataGridViewAutoSizeColumnMode.Fill);
            TV_Tonghop.Themcot("SDT_BN", "Số Điện Thoại", dt_tiepdoan, DataGridViewAutoSizeColumnMode.Fill);
           
            col = new DataGridViewTextBoxColumn();
            col.Visible = false;
            col.DataPropertyName = "GIOITINH_BN";
            col.Name = col.DataPropertyName;

           
            dt_tiepdoan.Columns.Add(col);
        }

        public SqlConnection getconnect()
        {
            return new SqlConnection("Data Source=NGUYENTY-PC\\SQLEXPRESS;Initial Catalog=QL_PHONGKHAM;Integrated Security=True");
           
        }

        private void frmtiepdon_Load(object sender, EventArgs e)
        {
            

           

            SqlCommand cmd, cmd1;
            DataTable dt, dt1;
            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            
            cmd = new SqlCommand(@"select a.MA_BN,a.HOTEN_BN,a.GIOITINH_BN,a.NAMSINH_BN,a.DIACHI_BN,a.SDT_BN,a.BAO_HIEM,b.Ten,a.Loai_BH ,case when GIOITINH_BN = 0 then N'Nữ' else 'Nam' end Gioitinh from BENH_NHAN a join Loai_BAO_HIEM b on a.Loai_BH = b.Ma", ketnoi);
            dt = new DataTable();
            dt.Load(cmd.ExecuteReader());
           
            dt_tiepdoan.DataSource = dt;

            if (ketnoi.State == ConnectionState.Closed)
                ketnoi.Open();

            cmd1 = new SqlCommand(@"select * from Loai_Bao_Hiem", ketnoi);
            dt1 = new DataTable();
            dt1.Load(cmd1.ExecuteReader());

            cmb_loaiBH.DataSource = dt1;
            cmb_loaiBH.DisplayMember = "Ten";
            cmb_loaiBH.ValueMember = "Ma";

            
        }
        
        private void bt_Them_Click(object sender, EventArgs e)
        {
            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            SqlCommand cmd = new SqlCommand("ThemBenhNhan", ketnoi);
            cmd.CommandType = CommandType.StoredProcedure;                                          // kieu cau truy van SQL la kieu StoredProcedure
            cmd.Parameters.Add("@mabn", SqlDbType.NChar).Value = cmb_mabenhnhan.Text;
            cmd.Parameters.Add("@hoten_bn", SqlDbType.NVarChar).Value = txb_tenbenhnhan.Text;
            cmd.Parameters.Add("@gioitinh_bn", SqlDbType.Bit).Value = rdb_Nam.Checked;

            cmd.Parameters.Add("@baohiem", SqlDbType.Bit).Value = ckb_co.Checked;
            cmd.Parameters.Add("@loaibaohiem", SqlDbType.Int).Value = cmb_loaiBH.SelectedValue;

            cmd.Parameters.Add("@namsinh_bn", SqlDbType.Date).Value = Convert.ToDateTime(dt_namsinhbenhnhan.Text); // mặc định thì dtnamsinh.text là kiểu string mình phải chuyển nó về kiểu date
            cmd.Parameters.Add("@diachi_bn", SqlDbType.NVarChar).Value = diachi.Text; 
            cmd.Parameters.Add("@sdt_bn", SqlDbType.VarChar).Value = sodt.Text;
            cmd.ExecuteNonQuery();
            frmtiepdon_Load(sender, e);
        }

        private void bt_Sua_Click(object sender, EventArgs e)
        {
            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            SqlCommand cmd = new SqlCommand("CapNhatBenhNhan", ketnoi);
            cmd.CommandType = CommandType.StoredProcedure;                           // kieu cau truy van SQL la kieu StoredProcedure
            cmd.Parameters.Add("@mabn", SqlDbType.NChar).Value = cmb_mabenhnhan.Text;
            cmd.Parameters.Add("@hoten_bn", SqlDbType.NVarChar).Value = txb_tenbenhnhan.Text;
            cmd.Parameters.Add("@gioitinh_bn", SqlDbType.Bit).Value = rdb_Nam.Checked;
            cmd.Parameters.Add("@baohiem", SqlDbType.Bit).Value = ckb_co.Checked;
            cmd.Parameters.Add("@loaibaohiem", SqlDbType.Int).Value = cmb_loaiBH.SelectedValue;
            cmd.Parameters.Add("@namsinh_bn", SqlDbType.Date).Value = Convert.ToDateTime(dt_namsinhbenhnhan.Text); // mặc định thì dtnamsinh.text là kiểu string
            cmd.Parameters.Add("@diachi_bn", SqlDbType.NVarChar).Value = diachi.Text;                              // mình phải chuyển nó về kiểu date
            cmd.Parameters.Add("@sdt_bn", SqlDbType.VarChar).Value = sodt.Text;
            cmd.ExecuteNonQuery();
            frmtiepdon_Load(sender, e);
        }

        private void bt_Xoa_Click(object sender, EventArgs e)
        {
            SqlConnection ketnoi = getconnect();
            ketnoi.Open();
            SqlCommand cmd = new SqlCommand("XoaBenhNhan", ketnoi);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@mabn", SqlDbType.NChar).Value = cmb_mabenhnhan.Text;
            cmd.ExecuteNonQuery();
            
            frmtiepdon_Load(sender, e);
        }

        int dong;
        
        private void dt_tiepdoan_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dong = e.RowIndex;
            cmb_mabenhnhan.Text = dt_tiepdoan.Rows[dong].Cells["Ma_BN"].Value.ToString();
            _mabn = cmb_mabenhnhan.Text;

            txb_tenbenhnhan.Text = dt_tiepdoan.Rows[dong].Cells["HoTen_BN"].Value.ToString();
            rdb_Nam.Checked = (bool)dt_tiepdoan.Rows[dong].Cells["Gioitinh_BN"].Value; 
            rdb_Nu.Checked = !rdb_Nam.Checked;
            ckb_co.Checked = (bool)dt_tiepdoan.Rows[dong].Cells["BAO_HIEM"].Value;
            
            cmb_loaiBH.SelectedValue = dt_tiepdoan.Rows[dong].Cells["Loai_BH"].Value; 
            // báo lỗi giống nhau trên các dòng này .
            dt_namsinhbenhnhan.Text = dt_tiepdoan.Rows[dong].Cells["NAMSINH_BN"].Value.ToString(); 
            sodt.Text = dt_tiepdoan.Rows[dong].Cells[5].Value.ToString();
            diachi.Text = dt_tiepdoan.Rows[dong].Cells[4].Value.ToString();
        }

        private void ckb_co_CheckedChanged(object sender, EventArgs e)
        {
            if (ckb_co.CheckState == CheckState.Checked)
            {
                lbLoaiBH.Visible = true;
                cmb_loaiBH.Visible = true;
            }
            else 
            {
                lbLoaiBH.Visible = false;
                cmb_loaiBH.Visible = false;
            }
        }

        private void btnKham_Click(object sender, EventArgs e)
        {
            frmkhambenh f = new frmkhambenh();
            f.txb_mabenhnhan.Text = txb_tenbenhnhan.Text;
            f._mabnKham = _mabn;
            f._tenbnKham = txb_tenbenhnhan.Text;
            f.ShowDialog();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {


            DataTable dtb = AddID.Ham_Tu_Tang.Select_Max("BENH_NHAN", "MA_BN");
            string chuoi = dtb.Rows[0]["id"] + "";
            string kq = AddID.Ham_Tu_Tang.AddID(chuoi, 2, 2);

            cmb_mabenhnhan.Text = kq;
        }

        private void cmb_mabenhnhan_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

       
       
    }
}
